package com.abing.system.controller;

import com.abing.common.annotation.Log;
import com.abing.common.controller.BaseController;
import com.abing.common.service.DictService;
import com.abing.common.utils.PageUtils;
import com.abing.common.utils.Query;
import com.abing.system.domain.RoleDO;
import com.abing.system.domain.UserDO;
import com.abing.system.service.RoleService;
import com.abing.system.service.UserService;
import org.apache.ibatis.annotations.Insert;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


@RequestMapping("/sys/user")
@Controller
public class UserController extends BaseController {
    private String prefix="system/user";
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;
    @Autowired
    DictService dictService;
    @RequiresPermissions("sys:user:user")
    @GetMapping("")
    String user(Model model) {
        return prefix + "/user";
    }


    @RequiresPermissions("sys:user:add")
    @Log("添加用户")
    @GetMapping("/add")
    String add(Model model){

        List<RoleDO> roleDOS=roleService.list();
        model.addAttribute("roles",roleDOS);
        return prefix+"/add";
    }


    @GetMapping("/personal")
    String personal(Model model){
        UserDO userDO=userService.get(getUserId());
        model.addAttribute("user",userDO);
        model.addAttribute("hobbyList",dictService.getHobbyList(userDO));
        model.addAttribute("sexList",dictService.getSexList());
        return prefix+"/personal";
    }


    @GetMapping("/list")
    @ResponseBody
    PageUtils list(@RequestParam Map<String,Object> params){
        //查询列表数据
        Query query=new Query(params);
        List<UserDO> sysUserList=userService.list(query);
        int total=userService.count(query);
        PageUtils pageUtils=new PageUtils(sysUserList,total);
        return pageUtils;
    }

}
